package com.flower.salary.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.flower.salary.common.utils.LogLevel;
import com.flower.salary.common.utils.SalaryException;
import com.flower.salary.domain.bo.DeptInfoBO;
import com.flower.salary.domain.entity.SysDept;
import com.flower.salary.domain.vo.DeptVO;
import com.flower.salary.mapper.SysDeptMapper;
import com.flower.salary.service.SysDeptService;
import com.flower.salary.service.SysLogService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lavender
 * @version 1.0
 * @date 2024-01-20 20:41
 */
@Service
public class SysDeptServiceImpl implements SysDeptService {

    @Autowired
    private SysLogService logService;

    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Override
    public List<SysDept> queryAll() {
        QueryWrapper q = new QueryWrapper();
        List<SysDept> list = sysDeptMapper.selectList(q);
        logService.insert("查看所有部门", LogLevel.INFO);
        return list;
    }

    @Override
    public int updateDeptInfo(DeptInfoBO infoBO) {
        SysDept deptVO = new SysDept();
        BeanUtils.copyProperties(infoBO,deptVO);
        int i = sysDeptMapper.updateById(deptVO);
        if(i==0){
            throw  new SalaryException("修改部门信息失败");
        }
        logService.insert("管理员修改部门信息", LogLevel.INFO);
        return 0;
    }

    @Override
    public int createDept(DeptInfoBO infoBO) {
        SysDept deptVO = new SysDept();
        BeanUtils.copyProperties(infoBO,deptVO);
        int i = sysDeptMapper.insert(deptVO);
        if(i==0){
            throw  new SalaryException("创建部门失败");
        }
        logService.insert("管理员创建部门", LogLevel.INFO);
        return 0;
    }

    @Override
    public int deleteDept(String id) {


        int i = sysDeptMapper.deleteById(id);
        if(i==0){
            throw  new SalaryException("删除部门信息失败");
        }
        logService.insert("删除部门", LogLevel.DANGETOUS);
        return 0;
    }

}
